package com.webchat.act.controller;

import com.webchat.act.service.ResourceBehaviorFactory;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.config.annotation.SafeClick;
import com.webchat.domain.vo.request.act.ResourceBehaviorRequestVO;
import com.webchat.rmi.act.IResourceBehaviorClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;


@RestController
public class ResourceBehaviorController implements IResourceBehaviorClient {


    @Override
    public APIResponseBean<Long> behavior(@RequestBody ResourceBehaviorRequestVO resourceBehaviorRequest) {

        resourceBehaviorRequest.validateRequestParam();

        // 根据具体互动行为类型走工厂模式获取具体的互动实现类，完成behavior操作
        long count = ResourceBehaviorFactory.getService(resourceBehaviorRequest.getBehaviorType())
                        .behavior(resourceBehaviorRequest.getUserId(),
                                    resourceBehaviorRequest.getResourceType(),
                                    resourceBehaviorRequest.getResourceId());
        return APIResponseBeanUtil.success(count);
    }

    @Override
    public APIResponseBean<Long> cancelBehavior(@RequestBody ResourceBehaviorRequestVO resourceBehaviorRequest) {

        resourceBehaviorRequest.validateRequestParam();

        // 根据具体互动行为类型走工厂模式获取具体的互动实现类，完成behavior操作
        long count = ResourceBehaviorFactory.getService(resourceBehaviorRequest.getBehaviorType())
                .cancelBehavior(resourceBehaviorRequest.getUserId(),
                                resourceBehaviorRequest.getResourceType(),
                                resourceBehaviorRequest.getResourceId());
        return APIResponseBeanUtil.success(count);
    }

    @Override
    public APIResponseBean<Map<Long, Long>> countBehavior(@RequestBody ResourceBehaviorRequestVO resourceBehaviorRequest) {
        // TODO 参数校验
        Map<Long, Long> countMap = ResourceBehaviorFactory.getService(resourceBehaviorRequest.getBehaviorType())
                .countBehavior(resourceBehaviorRequest.getResourceType(),
                        resourceBehaviorRequest.getResourceIds());
        return APIResponseBeanUtil.success(countMap);
    }

    @Override
    public APIResponseBean<Map<Long, Boolean>> isBehavior(@RequestBody ResourceBehaviorRequestVO resourceBehaviorRequest) {
        // TODO 参数校验
        Map<Long, Boolean> behaviorStatusMap = ResourceBehaviorFactory.getService(resourceBehaviorRequest.getBehaviorType())
                                                .isBehavior(resourceBehaviorRequest.getUserId(),
                                                        resourceBehaviorRequest.getResourceType(),
                                                        resourceBehaviorRequest.getResourceIds());
        return APIResponseBeanUtil.success(behaviorStatusMap);
    }

    @Override
    public APIResponseBean<Map<String, Set<String>>> listBehaviorUsers(@RequestBody ResourceBehaviorRequestVO resourceBehaviorRequest) {
        // TODO 参数校验
        Map<String, Set<String>> behaviorUsersMap = ResourceBehaviorFactory.getService(resourceBehaviorRequest.getBehaviorType())
                                                .listBehaviorUsers(resourceBehaviorRequest.getResourceType(),
                                                                   resourceBehaviorRequest.getResourceIds(),
                                                                   resourceBehaviorRequest.getSize());
        return APIResponseBeanUtil.success(behaviorUsersMap);
    }
}
